package com.tjian.fafa.user.base.friendRequest;

import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;

import com.tjian.fafa.user.base.bean.ChatEntity;
import com.tjian.fafa.user.base.bean.FrdReqNotifItemEntity;
import com.tjian.fafa.user.base.bean.FrdRequestEntity;
import com.tjian.fafa.user.base.bean.TabMsgItemEntity;
import com.tjian.fafa.user.base.bean.UserInfo;
import com.tjian.fafa.user.base.chatServices.FriendListInfo;
import com.tjian.fafa.user.base.mainBody.FrdRequestNotifActivity;
import com.tjian.fafa.user.base.mainBody.MainBodyActivity;
import com.tjian.fafa.user.base.mainBody.MainTabMsgPage;

public class FriendRequestService extends Service
{

    private static FriendRequestService mInstance;

    private UserInfo mRequester;
    private UserInfo mRequestee;
    private FriendRequestMsgReceiver mFriendRequestReceiver;

    public static FriendRequestService getInstance()
    {
        return mInstance;
    }

    @Override
    public IBinder onBind(Intent intent)
    {
        return null;
    }

    @Override
    public void onCreate()
    {
        super.onCreate();
        mInstance = this;

        IntentFilter ifter = new IntentFilter("fafa.example.hello.FRIEND_REQUEST_MSGS");
        mFriendRequestReceiver = new FriendRequestMsgReceiver();
        registerReceiver(mFriendRequestReceiver, ifter);
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        unregisterReceiver(mFriendRequestReceiver);
    }

    public void processFriendRequest(String msg0)
    {
        FrdRequestEntity reqEnt0 = new FrdRequestEntity(msg0);
        mRequester = reqEnt0.getRequester();//对方信息haoren4&#&10&#&0&#&0&#&1&#&1941&#&1&#&1&#&xx&#&xx&#&xx&#&imgs/avatar/2014/12/27/mmexport1420264475185.jpg&#&
        mRequestee = reqEnt0.getRequestee();//自己信息haoren2&#&3&#&1&#&1&#&0&#&1990&#&3&#&2&#&xx&#&xx&#&xx&#&imgs/avatar/2014/12/27/haoren2.jpg&#&
        Log.i("qqpath=", mRequester.getmAvatarImagePath());
        MainTabMsgPage.getInstance().onUpdateById(TabMsgItemEntity.FrdReqNotifId, mRequester.getAvatarId(),
                mRequester.getName(), mRequester.getName() + " 想成为你的好友",
                ChatEntity.genDate(), true, mRequester.getmAvatarImagePath());
        if (MainBodyActivity.getCurPage() == MainBodyActivity.mPageMsg)
        {
            MainTabMsgPage.getInstance().onUpdateView();
        }

        FrdRequestNotifActivity.newNotification(FrdReqNotifItemEntity.mTypeFrdReq, mRequester.getId(),
                mRequester.getAvatarId(), mRequester.getName(),
                mRequester.getName() + " 想成为你的好友", ChatEntity.genDate(), mRequester.toString(), mRequester.getmAvatarImagePath());
        if (FrdRequestNotifActivity.getInstance() != null && FrdRequestNotifActivity.getInstance().getIsCurPage() == true)
        {
            FrdRequestNotifActivity.getInstance().onUpdateView();
        }
    }

    public void processFriendRequestResponse(String msg0)
    {
        FrdRequestEntity reqEnt0 = new FrdRequestEntity(msg0);
        mRequestee = reqEnt0.getRequestee();

        if (reqEnt0.isAceepted())
        {
            MainTabMsgPage.getInstance().onUpdateById(TabMsgItemEntity.FrdReqNotifId, mRequestee.getAvatarId(),
                    mRequestee.getName(), mRequestee.getName() + " 接受了你的好友请求",
                    ChatEntity.genDate(), true, mRequestee.getmAvatarImagePath());
        }
        else
        {
            MainTabMsgPage.getInstance().onUpdateById(TabMsgItemEntity.FrdReqNotifId, mRequestee.getAvatarId(),
                    mRequestee.getName(), mRequestee.getName() + " 拒绝了你的好友请求",
                    ChatEntity.genDate(), true, mRequestee.getmAvatarImagePath());
        }

        if (MainBodyActivity.getCurPage() == MainBodyActivity.mPageMsg)
        {
            MainTabMsgPage.getInstance().onUpdateView();
        }

        String msg11;
        if (!reqEnt0.isAceepted())
        {
            msg11 = mRequestee.getName() + " has declined your request to be friends";
        }
        else
        {
            msg11 = mRequestee.getName() + " has accepted your request to be friends";
            FriendListInfo.getFriendListInfo().uponMakeNewFriend(mRequestee);
        }
        FrdRequestNotifActivity.newNotification(FrdReqNotifItemEntity.mTypeFrdReqResult, mRequestee.getId(),
                mRequestee.getAvatarId(), mRequestee.getName(),
                msg11, ChatEntity.genDate(), mRequestee.toString(), mRequestee.getmAvatarImagePath());
        if (FrdRequestNotifActivity.getInstance() != null && FrdRequestNotifActivity.getInstance().getIsCurPage() == true)
        {
            FrdRequestNotifActivity.getInstance().onUpdateView();
        }
    }

}
